package edu.wtbu.cs.bishe.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import edu.wtbu.cs.bishe.domain.Goodsinfo;
import edu.wtbu.cs.bishe.domain.User;
import org.apache.ibatis.annotations.*;

@Mapper
public interface UserMapper extends BaseMapper<User> {
    // 插入用户
    @Insert("INSERT INTO users(email, password, enabled, verification_code, code_expiry_time, code_attempts) " +
            "VALUES(#{email}, #{password}, #{enabled}, #{verificationCode}, #{codeExpiryTime}, #{codeAttempts})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertUser(User user);

    // 更新用户
    @Update("UPDATE users SET " +
            "email = #{email}, " +
            "password = #{password}, " +
            "enabled = #{enabled}, " +
            "verification_code = #{verificationCode}, " +
            "code_expiry_time = #{codeExpiryTime}, " +
            "code_attempts = #{codeAttempts} " +
            "WHERE id = #{id}")
    int updateUser(User user);

    // 根据邮箱查询
    @Select("SELECT * FROM users WHERE email = #{email}")
    User findByEmail(String email);

    // 验证码尝试次数+1
    @Update("UPDATE users SET code_attempts = code_attempts + 1 WHERE email = #{email}")
    int incrementAttempts(String email);

    void updateUserSelective(User user);
}